package com.ht.pms.core.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ht.pms.core.common.exception.ServiceException;
import com.ht.pms.core.dao.mapper.RecruitDetailMapper;
import com.ht.pms.core.dto.PageResult;
import com.ht.pms.core.dto.recruit.*;
import com.ht.pms.core.service.RecruitService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class RecruitServiceImpl extends ServiceImpl<RecruitDetailMapper, RecruitDetail> implements RecruitService {

    @Resource
    private RecruitDetailMapper recruitDetailMapper;


//    @Override
//    public PageResult recruitList(RecruitQueryDto dto) throws Exception {
//        Integer typeId = StringUtils.isEmpty(dto.getTypeId()) ? null : Integer.parseInt(dto.getTypeId());
//        String stationName = StringUtils.isBlank(dto.getStationName()) ? null : "%" + dto.getStationName() + "%";
//        String stationPlace = StringUtils.isBlank(dto.getStationPlace()) ? null : "%" + dto.getStationPlace() + "%";
//
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String startTime = dto.getStartTime() == null ? null : sdf.format(dto.getStartTime());
//        String endTime = dto.getEndTime() == null ? null : sdf.format(dto.getEndTime());
//
//        int pageSize = dto.getLimit() > 0 ? dto.getLimit() : 10;
//        int pageNum = dto.getPage() > 0 ? (dto.getPage() - 1) * pageSize : 0;
//        List<RecruitResultDto> list = recruitDetailMapper.recruitList(typeId, stationName, stationPlace, startTime, endTime, pageNum, pageSize);
//        int count = recruitDetailMapper.countRecruit(typeId, stationName, stationPlace, startTime, endTime);
//        PageResult pageResult = PageResult.success(list, count);
//        return pageResult;
//    }

    @Override
    public PageResult recruitListForQuery(String typeId, String stationName, String stationPlace, String startTime, String endTime, String state, int page, int limit) {
        Integer itypeId = StringUtils.isEmpty(typeId) ? null : Integer.parseInt(typeId);
        String sstationName = StringUtils.isBlank(stationName) ? null : "%" + stationName + "%";
        String sstationPlace = StringUtils.isBlank(stationPlace) ? null : "%" + stationPlace + "%";

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sstartTime = StringUtils.isBlank(startTime) ? null : startTime;
        String sendTime = StringUtils.isBlank(endTime) ? null : endTime;

        Integer istate = StringUtils.isEmpty(state) ? null : Integer.parseInt(state);
        int pageSize = limit > 0 ? limit : 10;
        int pageNum = page > 0 ? (page - 1) * pageSize : 0;
        List<RecruitResultDto> list = recruitDetailMapper.recruitList(itypeId, sstationName, sstationPlace, sstartTime, sendTime, istate, pageNum, pageSize);
        int count = recruitDetailMapper.countRecruit(itypeId, sstationName, sstationPlace, sstartTime, sendTime, istate);
        PageResult pageResult = PageResult.success(list, count);
        return pageResult;
    }

    @Override
    public RecruitDetailWeb recruitQuery(String id) throws Exception {

        Integer iid = Integer.parseInt(id);
        RecruitDetailWeb recruitDetail = recruitDetailMapper.selectRecruitDetailWeb(iid);
        return recruitDetail;
    }

    @Override
    public void addOrEditRecruit(String opType, String id, String typeId, String stationName, String stationPlace, String stationSalary, String stationNumer, Date startTime, Date endTime, String state, String stationSeniority, String stationDuty) throws Exception {
        int type = Integer.parseInt(opType);
        RecruitDetail recruitDetail = new RecruitDetail();
        recruitDetail.setTypeId(Integer.parseInt(typeId));
        recruitDetail.setStationName(stationName);
        recruitDetail.setStationPlace(stationPlace);
        recruitDetail.setStationSalary(stationSalary);
        recruitDetail.setStationNumer(stationNumer);
        recruitDetail.setStationSeniority(stationSeniority);
        recruitDetail.setStationDuty(stationDuty);
        recruitDetail.setStartTime(startTime);
        recruitDetail.setEndTime(endTime);
        if (type == 1) {//新增

            recruitDetailMapper.insert(recruitDetail);
        } else if (type == 2) {
            recruitDetail.setId(Integer.parseInt(id));
            recruitDetailMapper.updateById(recruitDetail);
        }

    }

    @Override
    public void addOrEditEntityRecruit(RecruitDetailQuery recruitDetailQuery) throws Exception {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date startTime = sdf.parse(recruitDetailQuery.getStartTime());
        Date endTime = sdf.parse(recruitDetailQuery.getEndTime());

        if (startTime.getTime() > endTime.getTime()) {
            throw new ServiceException("开始时间不能晚于结束时间", "执行失败");
        }
        if (!StringUtils.isBlank(recruitDetailQuery.getStationNumer()) && recruitDetailQuery.getStationNumer().length() > 30) {

            throw new ServiceException("招聘人数必须小于30个字符", "执行失败");

        }
        int type = Integer.parseInt(recruitDetailQuery.getOpType());
        RecruitDetail recruitDetail = new RecruitDetail();
        recruitDetail.setTypeId(Integer.parseInt(recruitDetailQuery.getTypeId()));
        recruitDetail.setStationName(recruitDetailQuery.getStationName());
        recruitDetail.setStationPlace(recruitDetailQuery.getStationPlace());
        recruitDetail.setStationSalary(recruitDetailQuery.getStationSalary());
        recruitDetail.setStationNumer(recruitDetailQuery.getStationNumer());
        recruitDetail.setStationSeniority(recruitDetailQuery.getStationSeniority());
        recruitDetail.setStationDuty(recruitDetailQuery.getStationDuty());
        recruitDetail.setLastUpdateTime(new Date());

        if (recruitDetailQuery.getState() == null) {
            recruitDetail.setState(0);
        } else {
            recruitDetail.setState(Integer.parseInt(recruitDetailQuery.getState()));
        }


        recruitDetail.setStartTime(startTime);
        recruitDetail.setEndTime(endTime);
        if (type == 1) {//新增

            recruitDetail.setCreateTime(new Date());
            recruitDetail.setCreateName(recruitDetailQuery.getCreateName());
            recruitDetailMapper.insert(recruitDetail);
        } else if (type == 2) {
            recruitDetail.setId(Integer.parseInt(recruitDetailQuery.getId()));
            recruitDetailMapper.updateById(recruitDetail);
        }
    }

    @Override
    public List<ProvinceCitys> getProviceCity(String provinceId) throws Exception {
        List<ProvinceCitys> provinceCitysList = null;
        if (StringUtils.isBlank(provinceId)) {
            provinceCitysList = recruitDetailMapper.queryProvices();
        } else {
            provinceCitysList = recruitDetailMapper.queryCityByProvinceId(provinceId);
        }
        return provinceCitysList;
    }

    @Override
    public List<RecruitDetailApp> recruitListApp(RecruitDetailAppReq recruitDetailAppReq) throws Exception {
        Integer typeId = null;
        if (!StringUtils.isBlank(recruitDetailAppReq.getTypeId()) && Integer.parseInt(recruitDetailAppReq.getTypeId()) != 0) {
            typeId = Integer.parseInt(recruitDetailAppReq.getTypeId());
        }

        String stationName = StringUtils.isBlank(recruitDetailAppReq.getStationName()) ? null : "%" + recruitDetailAppReq.getStationName() + "%";
        String cityId = StringUtils.isBlank(recruitDetailAppReq.getCityId()) ? null : recruitDetailAppReq.getCityId();
        return recruitDetailMapper.recruitListApp(typeId, stationName, cityId);
    }

    @Override
    public List<Province> getProviceCityApp() throws Exception {

        return recruitDetailMapper.getProviceCityApp();
    }


}
